package com.hhe.datax_transfer.controller;

import com.hhe.datax_transfer.cache.ExecuteJobsQueue;
import com.hhe.datax_transfer.common.JsonResult;
import com.hhe.datax_transfer.common.ReceiptDefine;
import com.hhe.datax_transfer.model.DataXJsonExecuteModel;
import com.hhe.datax_transfer.model.DataxJsonQueryFilterModel;
import com.hhe.datax_transfer.model.TransferDataModel;
import com.hhe.datax_transfer.service.TransferOperation;
import com.hhe.datax_transfer.utils.validation.groups.UpdateGroups;
import com.sun.media.sound.SoftTuning;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;


/**
 * 数据迁移操作页面控制层
 *
 * @author HHe
 * @version V1.0
 * @date 2021/1/8 15:06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/transfer/operate")
@AllArgsConstructor(onConstructor_ = {@Autowired})
public class TransferOperateViewController {
    private TransferOperation transferOperation;

    /**
     * 返回文件列表目录
     *
     * @version V1.0
     * @author HHe
     * @date 2021/1/26 10:52
     */
    @RequestMapping(value = "/getjoblistdirectory", method = RequestMethod.POST)
    public JsonResult getJobListDirectory(@RequestBody DataxJsonQueryFilterModel dataxJsonQueryFilterModel) {
        return new JsonResult("200", ReceiptDefine.SELECT_SUCC, transferOperation.getJobListDirectory(dataxJsonQueryFilterModel));
    }

    /**
     * 查询DataX配置文件列表
     *
     * @version V1.0
     * @author HHe
     * @date 2021/1/12 10:29
     */
    @RequestMapping(value = "/getdataxjoblist", method = RequestMethod.POST)
    public JsonResult getDataXJobList(@RequestBody DataxJsonQueryFilterModel dataxJsonQueryFilterModel) {
        return new JsonResult("200", ReceiptDefine.SELECT_SUCC, transferOperation.getDataXJobList(dataxJsonQueryFilterModel));
    }

    /**
     * 修改配置文件Json信息
     *
     * @version V1.0
     * @author HHe
     * @date 2021/1/12 10:30
     */
    @Validated(UpdateGroups.class)
    @RequestMapping(value = "/updatedataxjsonmess", method = RequestMethod.POST)
    public JsonResult updateDataXJsonMess(@RequestBody @Valid TransferDataModel transferDataModel) {
        return new JsonResult("200", ReceiptDefine.UPDATE_SUCC, transferOperation.updateDataXJsonMess(transferDataModel));
    }

    /**
     * 写入配置文件信息
     *
     * @version V1.0
     * @author HHe
     * @date 2021/1/13 10:12
     */
    @RequestMapping(value = "/writedataxjsonmess", method = RequestMethod.POST)
    public JsonResult writeDataXJsonMess() {
        return new JsonResult("200", ReceiptDefine.WRITE_SUCC, transferOperation.writeDataXJsonMess());
    }

    /**
     * 执行配置文件
     *
     * @version V1.0
     * @author HHe
     * @date 2021/1/13 10:12
     */
    @RequestMapping(value = "/executedataxjson", method = RequestMethod.POST)
    public JsonResult executeDataXJson(@RequestBody DataXJsonExecuteModel dataXJsonExecuteModel) {
        return new JsonResult("200", ReceiptDefine.EXECUTE_SUCC, transferOperation.executeDataXJson(dataXJsonExecuteModel));
    }

    /**
     * 终止DataX执行任务
     *
     * @version V1.0
     * @author HHe
     * @date 2021/3/5 12:22
     */
    @RequestMapping(value = "/interruptdataxjsonjob", method = RequestMethod.POST)
    public JsonResult interruptDataXJsonJob(@RequestBody TransferDataModel transferDataModel) {
        return new JsonResult("200", ReceiptDefine.EXECUTE_SUCC, transferOperation.interruptDataXJsonJob(transferDataModel));
    }

    /**
     * 删除执行队列任务
     *
     * @version V1.0
     * @author HHe
     * @date 2021/3/5 12:22
     */
    @RequestMapping(value = "/deleteexecutequeue", method = RequestMethod.POST)
    public JsonResult deleteExecuteQueue(@RequestBody TransferDataModel transferDataModel) {
        return new JsonResult("200", ReceiptDefine.EXECUTE_SUCC, transferOperation.deleteExecuteQueue(transferDataModel));
    }

    /**
     * 查看配置文件内容
     *
     * @version V1.0
     * @author HHe
     * @date 2021/1/15 17:12
     */
    @RequestMapping(value = "/dataxjsonfileview", method = RequestMethod.POST)
    public void dataxJsonFileView(@RequestBody TransferDataModel dataXJsonExecuteModel, HttpServletResponse response) throws IOException {
        transferOperation.dataxJsonFileView(dataXJsonExecuteModel, response);
    }

    /**
     * 查看配置文件对应日志
     *
     * @version V1.0
     * @author HHe
     * @date 2021/1/15 17:12
     */
    @RequestMapping(value = "/dataxjsonlogview", method = RequestMethod.POST)
    public void dataxJsonLogView(@RequestBody TransferDataModel dataXJsonExecuteModel, HttpServletResponse response) throws IOException {
        transferOperation.dataxJsonLogView(dataXJsonExecuteModel, response);
    }


    /**
     * DataX配置文件执行状态
     *
     * @version V1.0
     * @author HHe
     * @date 2021/3/5 12:21
     */
    @RequestMapping(value = "/dataxjsonrunstatus", method = RequestMethod.POST)
    public JsonResult dataxJsonRunStatus(@RequestBody TransferDataModel transferDataModel) {
        return new JsonResult("200", ReceiptDefine.EXECUTE_SUCC, transferOperation.dataxJsonRunStatus(transferDataModel));
    }

    @RequestMapping(value = "/test1", method = RequestMethod.POST)
    public JsonResult test1(@RequestBody TransferDataModel transferDataModel) {
//        for (int i = 0; i < 10; i++) {
//            ExecuteJobsQueue.put(""+i);
//        }
//        ExecuteJobsQueue.executeSwitch = true;
        return new JsonResult("200", ReceiptDefine.EXECUTE_SUCC, null);
    }

    @RequestMapping(value = "/test2", method = RequestMethod.POST)
    public JsonResult test2(@RequestBody TransferDataModel transferDataModel) {
//        System.out.println(ExecuteJobsQueue.getNum());
        return new JsonResult("200", ReceiptDefine.EXECUTE_SUCC, null);
    }
}
